var groupInfo={
	"groupName": null,
	"details": null,
	"avatarUrl": null,
	"managerQrCodeUrl": null,
	"qrCodeUrl": null,
	"qrcodeText":null
};
var uploadFile = {
	//上传头像图片
	uploadAvatar: function(id, callBack){
		var upload=new AjaxUpload('#'+id, {  
		    action: SystemConfig.baseServiceUrl+"upload/avatarImage",  //url
		    name:'imageFile',
		    autoSubmit:false,
		    onSubmit: function (file, ext) {   //上传时事件
				//$('#upload_local').text("上传中...");
	   			$("body").modelHanding(true);
		    },  
		    hoverClass:'upload_batch',
		    onChange: function (file, ext) {  
		        if(!Validation.isImageFile(file)){
					$("#mobileInfo_span").html("请选择图片文件");
					$(".message_box").show().delay(800).hide(0);
					return;
		        }else{
					upload.submit();
		        }
		    },  
		    success: function (data) {
		    	callBack(data);
		    },
		    exception: function (data) {
		    	$.closeHanding();//关闭遮罩模态
		    	if(!SystemUtil.isNull(data)){
		    		$("#mobileInfo_span").html(data.responseStatus.message);
					$(".message_box").show().delay(800).hide(0);
		    	}
		    }
	  });  
	},
	//上传二维码图片
	uploadQrcode: function(id, callBack){
		var upload=new AjaxUpload('#'+id, {  
		    action: SystemConfig.baseServiceUrl+"upload/qrCodeImage",  //url
		    name:'qrcodeFile',
		    autoSubmit:false,
		    onSubmit: function (file, ext) {   //上传时事件
	   			$("body").modelHanding(true);
		    },  
		    hoverClass:'upload_batch',
		    onChange: function (file, ext) {  
		        if(!Validation.isImageFile(file)){
					$("#mobileInfo_span").html("请选择图片文件");
					$(".message_box").show().delay(800).hide(0);
					return;
		        }else{
					upload.submit();
		        }
		    },  
		    success: function (data) {
		    	callBack(data);
		    },
		    exception: function (data) {
		    	$.closeHanding();//关闭遮罩模态
		    	if(!SystemUtil.isNull(data)){
					$("#mobileInfo_span").html(data.responseStatus.message);
					$(".message_box").show().delay(800).hide(0);
		    	}
		    }
	  });  
	},
	//上传群主二维码图片
	updateManagerQrCode: function(id, callBack){
		var upload=new AjaxUpload('#'+id, {  
		    action: SystemConfig.baseServiceUrl+"upload/managerQrCode",  //url
		    name:'managerQrCodeFile',
		    autoSubmit:false,
		    onSubmit: function (file, ext) {   //上传时事件
	   			$("body").modelHanding(true);
		    },  
		    hoverClass:'upload_batch',
		    onChange: function (file, ext) {  
		        if(!Validation.isImageFile(file)){
					$("#mobileInfo_span").html("请选择图片文件");
					$(".message_box").show().delay(800).hide(0);
					return;
		        }else{
					upload.submit();
		        }
		    },  
		    success: function (data) {
		    	callBack(data);
		    },
		    exception: function (data) {
		    	$.closeHanding();//关闭遮罩模态
		    	if(!SystemUtil.isNull(data)){
					$("#mobileInfo_span").html(data.responseStatus.message);
					$(".message_box").show().delay(800).hide(0);
		    	}
		    }
	  });  
	}
};
$(function() {
	$(".message_box").hide();
	$('#groupName').blur(function() {
		return group.verifyGroupName();
	});
	$('#details').blur(function() {
		return group.verifyDetails();
	});
	$('.new_sublit').click(function () {
		if(group.verifyGroupInfo()){
			$.ajax({
				type : "POST",
				url : SystemConfig.baseServiceUrl + "group/createGroupInfo",
				data : $.toJSON({'groupInfo': groupInfo}),
				contentType : "application/json; charset=UTF-8",
				dataType : "json",
				success : function(resp) {
					window.location.href = SystemConfig.baseHtmlUrl+"index.html";
				},
				exception : function(resp) {
					var message = resp.responseStatus.message;
					$(".message_box").show().delay(800).hide(0);
				}
			});			
		}
	});
	uploadFile.uploadAvatar("uploadGroupAvatarUrl",function(data){
		if(!SystemUtil.isNull(data)){
    		$("#uploadGroupAvatarUrl").attr('src', SystemUtil.buildImageUrl(data.fileUrl, "400x400"));
			groupInfo.avatarUrl = data.fileUrl;
    	}
	});
	uploadFile.updateManagerQrCode("updateManagerQrCodeUrl",function(data){
		if(!SystemUtil.isNull(data)){
    		$("#updateManagerQrCodeUrl").attr('src', SystemUtil.buildImageUrl(data.fileUrl, "400x400"));
			groupInfo.managerQrCodeUrl = data.fileUrl;
    	}
	});
	uploadFile.uploadQrcode("uploadGroupGrCodeUrl",function(data){
		if(!SystemUtil.isNull(data)){
    		$("#uploadGroupGrCodeUrl").attr('src', SystemUtil.buildImageUrl(data.fileUrl, "400x400"));
			groupInfo.qrCodeUrl = data.fileUrl;
			groupInfo.qrcodeText = data.qrcodeText;
    	}
	});
});

var group = {
	verifyGroupName : function() {
		var groupName = $('#groupName').val();
		if (!Validation.isNotEmpty(groupName)) {
			$("#mobileInfo_span").html("群名称不能为空");
			$(".message_box").show().delay(800).hide(0);
			return false;
		}else{
			$(".message_box").hide();
			return true;
		}
	},
	verifyDetails : function() {
		var details = $('#details').val();
		if (!Validation.isNotEmpty(details)) {
			$("#mobileInfo_span").html("群名称不能为空");
			$(".message_box").show().delay(800).hide(0);
			return false;
		}else{
			$(".message_box").hide();
			return true;
		}
	},
	verifyGroupInfo : function() {
		var groupName = $('#groupName').val();
		if (!Validation.isNotEmpty(groupName)) {
//			$(".message_box").show();
			$("#mobileInfo_span").html("群名称不能为空");
			$(".message_box").show().delay(800).hide(0);
			return false;
		}
		var details = $('#details').val();
		if (!Validation.isNotEmpty(details)) {
//			$(".message_box").show();
			$("#mobileInfo_span").html("群说明不能为空");
			$(".message_box").show().delay(800).hide(0);
			return false;
		}
		if(groupInfo==null){
//			$(".message_box").show();
			$("#mobileInfo_span").html("请上传图片");
			$(".message_box").show().delay(800).hide(0);
			return false;
		}
		if(groupInfo.avatarUrl==null){
//			$(".message_box").show();
			$("#mobileInfo_span").html("请上传上传群封面");
			$(".message_box").show().delay(800).hide(0);
			return false;
			
		}
		if(groupInfo.managerQrCodeUrl==null){
//			$(".message_box").show();
			$("#mobileInfo_span").html("请上传上传群主二维码图片");
			$(".message_box").show().delay(800).hide(0);
			return false;
		}
		if(groupInfo.qrCodeUrl==null){
//			$(".message_box").show();
			$("#mobileInfo_span").html("请上传上传上传群二维码图片");
			$(".message_box").show().delay(800).hide(0);
			return false;
		}
		groupInfo.groupName = groupName;
		groupInfo.details = details;
		$(".message_box").hide();
		$("#mobileInfo_span").html("");
		return true;
	}
};